package com.example.game;

import com.example.game.entity.Bullet;
import com.example.game.entity.Enemy;
import com.example.game.entity.Player;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GamePanel extends JPanel implements Runnable {
    private static final int PANEL_WIDTH = 800;
    private static final int PANEL_HEIGHT = 600;
    private static final int GROUND_Y = 500;
    
    private Player player;
    private List<Enemy> enemies;
    private List<Bullet> bullets;
    private boolean isRunning;
    private Thread gameThread;
    private boolean leftPressed;
    private boolean rightPressed;
    
    public GamePanel() {
        setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
        setBackground(Color.BLACK);
        setFocusable(true);
        
        initGame();
        setupKeyListener();
    }
    
    private void initGame() {
        player = new Player(100, GROUND_Y - 50);
        enemies = new ArrayList<>();
        bullets = new ArrayList<>();
        
        // Add some initial enemies
        enemies.add(new Enemy(400, GROUND_Y - 40));
        enemies.add(new Enemy(600, GROUND_Y - 40));
        
        isRunning = true;
    }
    
    private void setupKeyListener() {
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_LEFT:
                        leftPressed = true;
                        break;
                    case KeyEvent.VK_RIGHT:
                        rightPressed = true;
                        break;
                    case KeyEvent.VK_SPACE:
                        player.jump();
                        break;
                    case KeyEvent.VK_X:
                        shoot();
                        break;
                }
            }
            
            @Override
            public void keyReleased(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_LEFT:
                        leftPressed = false;
                        break;
                    case KeyEvent.VK_RIGHT:
                        rightPressed = false;
                        break;
                }
            }
        });
    }
    
    private void shoot() {
        int bulletX = player.isFacingRight() ? player.getX() + 50 : player.getX() - 5;
        bullets.add(new Bullet(bulletX, player.getY() + 25, player.isFacingRight()));
    }
    
    @Override
    public void addNotify() {
        super.addNotify();
        gameThread = new Thread(this);
        gameThread.start();
    }
    
    @Override
    public void run() {
        while (isRunning) {
            update();
            repaint();
            
            try {
                Thread.sleep(16); // Approximately 60 FPS
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    private void update() {
        // Update player movement
        if (leftPressed) {
            player.moveLeft();
        }
        if (rightPressed) {
            player.moveRight();
        }
        
        player.update();
        
        // Update bullets
        Iterator<Bullet> bulletIterator = bullets.iterator();
        while (bulletIterator.hasNext()) {
            Bullet bullet = bulletIterator.next();
            bullet.update();
            
            // Remove bullets that are off screen
            if (bullet.isOffScreen(PANEL_WIDTH)) {
                bulletIterator.remove();
                continue;
            }
            
            // Check for bullet collisions with enemies
            Iterator<Enemy> enemyIterator = enemies.iterator();
            while (enemyIterator.hasNext()) {
                Enemy enemy = enemyIterator.next();
                if (enemy.isHit(bullet.getBounds())) {
                    enemyIterator.remove();
                    bulletIterator.remove();
                    break;
                }
            }
        }
        
        // Update enemies
        for (Enemy enemy : enemies) {
            enemy.update();
        }
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        // Draw ground
        g.setColor(Color.GREEN);
        g.fillRect(0, GROUND_Y, PANEL_WIDTH, PANEL_HEIGHT - GROUND_Y);
        
        // Draw player
        player.draw(g);
        
        // Draw bullets
        for (Bullet bullet : bullets) {
            bullet.draw(g);
        }
        
        // Draw enemies
        for (Enemy enemy : enemies) {
            enemy.draw(g);
        }
        
        // Draw game info
        g.setColor(Color.WHITE);
        g.drawString("Enemies remaining: " + enemies.size(), 20, 30);
    }
}
